<?php

/**
 * 基础数据统计
 *
 * @author genjie.wang
 */

require_once 'CommonController.php';

class Admin_BaseDataCountController extends Admin_CommonController
{    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_DayAreaData();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction(){
        $time = time();
        $cookieModel = new Application_Model_Cookie();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        if ($this->_request->isPost()) {
            $vars['addressIsDisp'] = $this->_request->get('addressIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('addressIsDisp_order', $vars['addressIsDisp'], $time + 3600 * 24 * 30);
            $vars['appVersionIsDisp'] = $this->_request->get('appVersionIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('appVersionIsDisp_order', $vars['appVersionIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipIsDisp'] = $this->_request->get('ipIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipIsDisp_order', $vars['ipIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipProvinceIsDisp'] = $this->_request->get('ipProvinceIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipProvinceIsDisp_order', $vars['ipProvinceIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipCityIsDisp'] = $this->_request->get('ipCityIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipCityIsDisp_order', $vars['ipCityIsDisp'], $time + 3600 * 24 * 30);
            $vars['sessionIdIsDisp'] = $this->_request->get('sessionIdIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('sessionIdIsDisp_order', $vars['sessionIdIsDisp'], $time + 3600 * 24 * 30);
            $vars['udidIsDisp'] = $this->_request->get('udidIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('udidIsDisp_order', $vars['udidIsDisp'], $time + 3600 * 24 * 30);
            $vars['verIsDisp'] = $this->_request->get('verIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('verIsDisp_order', $vars['verIsDisp'], $time + 3600 * 24 * 30);
            $vars['userAgentIsDisp'] = $this->_request->get('userAgentIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('userAgentIsDisp_order', $vars['userAgentIsDisp'], $time + 3600 * 24 * 30);
        } else {
            $vars['addressIsDisp'] = $cookieModel->getCookie('addressIsDisp_order') ? 1 : 0;
            $vars['appVersionIsDisp'] = $cookieModel->getCookie('appVersionIsDisp_order') ? 1 : 0;
            $vars['ipProvinceIsDisp'] = $cookieModel->getCookie('ipProvinceIsDisp_order') ? 1 : 0;
            $vars['ipCityIsDisp'] = $cookieModel->getCookie('ipCityIsDisp_order') ? 1 : 0;
            $vars['ipIsDisp'] = $cookieModel->getCookie('ipIsDisp_order') ? 1 : 0;
            $vars['sessionIdIsDisp'] = $cookieModel->getCookie('sessionIdIsDisp_order') ? 1 : 0;
            $vars['udidIsDisp'] = $cookieModel->getCookie('udidIsDisp_order') ? 1 : 0;
            $vars['verIsDisp'] = $cookieModel->getCookie('verIsDisp_order') ? 1 : 0;
            $vars['userAgentIsDisp'] = $cookieModel->getCookie('userAgentIsDisp_order') ? 1 : 0;
        }
        $this->view->memberRows = array();
        $vars['areas'] = trim($this->_request->get('areas'));
        $vars['province'] = trim($this->_request->get('province'));
        $vars['city'] = trim($this->_request->get('city'));
        $vars['county'] = trim($this->_request->get('county'));
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $vars['source'] = trim($this->_request->get('source'));
        if(empty($vars['startTime']) && !$this->_request->isPost())
            $vars['startTime'] = date("Y-m-01",strtotime("-1 day"));
        if(empty($vars['endTime']) && !$this->_request->isPost())
            $vars['endTime'] = date("Y-m-d",strtotime("now"));
        $urls = $vars;
        
        $total[] = $wheres[] = "`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."'";
        if ($vars['province'] != '') {
            $wheres[] = "`province` LIKE '%" . addslashes($vars['province']) . "%'";
            $total[] = "`province` LIKE '%" . addslashes($vars['province']) . "%'";
        }
        if ($vars['city'] != '') {
            $wheres[] = "`city` LIKE '%" . addslashes($vars['city']) . "%'";
            $total[] = "`city` LIKE '%" . addslashes($vars['city']) . "%'";
        }
        if ($vars['county'] != '') {
            $wheres[] = "`county` LIKE '%" . addslashes($vars['county']) . "%'";
            $total[] = "`county` LIKE '%" . addslashes($vars['county']) . "%'";
        }
        if ($vars['osName'] != '' && $vars['osName']!= '其他') {
            $wheres[] = "`osName` = '" . addslashes($vars['osName']) . "'";
            $total[] = "`osName` = '" . addslashes($vars['osName']) . "'";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '" . addslashes($vars['sourceId']) . "'";
            $total[] = "`sourceId` = '" . addslashes($vars['sourceId']) . "'";
        }elseif ($vars['source'] != '') {
            $wheres[] = "`sourceId` in (SELECT source.sourceid FROM mob_member mem LEFT JOIN mob_source source ON mem.code = source.memberCode  WHERE name = '{$vars['source']}' AND productcode='". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."')";
            $total[] = "`sourceId` in (SELECT source.sourceid FROM mob_member mem LEFT JOIN mob_source source ON mem.code = source.memberCode  WHERE name = '{$vars['source']}' AND productcode='". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."')";
        }
        if ($vars['startTime'] != '') {
            $wheres[] = "`date` >= '{$vars['startTime']}' ";
            $total[] = "`date` < '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`date` <= '{$vars['endTime']}' ";
        }
        if ($vars['areas'] != '') {
            $wheres[] = "`province` = '{$vars['areas']}' ";
        }
        if ($vars['osName'] == '其他') {
            $wheres[] = "`osName` = ''";
            $total[] = "`osName` = ''";
        }
//获取操作系统
        $Model = new Application_Model_DayAreaData();
        $sql = "SELECT osName FROM `{$Model->getTableName()}`" . " WHERE`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."' GROUP BY `osName` ";
        $osName = $Model->getAdapter()->fetchAll($sql);
        $this->view->osNameRows = $osName;
//地区
        //$this->view->memberRows = array('name'=>array());
        $sql = "SELECT province FROM `{$Model->getTableName()}`" . " WHERE`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."' GROUP BY `province` ";
        $province = $Model->getAdapter()->fetchAll($sql);
        $this->view->memberRows = $province;
//渠道
        $sql = "SELECT `name`,`code` FROM (
                    SELECT `code`,mem.name FROM mob_member AS mem LEFT JOIN mob_source AS source ON mem.code  = source.memberCode WHERE source.productCode='vancl'
                )tmp
                GROUP BY `name`,`code`";
        $this->view->channels = $Model->getAdapter()->fetchAll($sql);
//查询
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $accumulative = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $total) . ' ' : '';
        $sql = "SELECT `date` AS date,SUM(installNum) AS cnt, SUM(bootClientNum) AS boot, SUM(bootNum) AS bootNum, SUM(orderNum) AS orderNum, SUM(orderAmount ) AS orderAmount FROM `{$Model->getTableName()}` {$where} GROUP BY `date` ";
        $this->view->rows = $Model->getAdapter()->fetchAll($sql);

        $sql = "SELECT `date` AS date,SUM(installNum) AS Alluser FROM `{$Model->getTableName()}` {$accumulative}";
        $alluser = $Model->getAdapter()->fetchAll($sql);
        $this->view->all = $alluser[0];
        $this->view->vars = $vars;
    }

    /**
     * 更新sourceids
     * 
     * @return void
     */
    public function sourceidsAction() 
    {
        $source = trim($this->_request->getPost('source'));
        $sourceModel = new Application_Model_Source();
        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $rows = $sourceModel ->getAdapter()->fetchAll("SELECT mm.name,ms.`sourceId`  FROM `mob_source` AS ms INNER JOIN `mob_member` AS mm ON ms.memberCode=mm.code WHERE ms.productCode='{$productCode}' AND ms.`sourceId`!='' AND mm.`name`='{$source}'   Group By mm.name,ms.`sourceId`");
        $res = array();
        foreach($rows as $row) {
            $res[]=$row['sourceId'];
        }
        $json_string=json_encode($res);
        echo $json_string;
        exit();
        
    }

}